Component({
  properties: {
    // 当前页面路径
    currentPage: {
      type: String,
      value: 'pages/index/index'
    }
  },

  data: {
    navTitle: '校园新闻网',
    showBackButton: false,
    showMenuButton: false,
    showSearchIcon: false,
    showClearHistoryButton: false
  },

  observers: {
    'currentPage': function(pagePath) {
      this.updateNavBar(pagePath)
    }
  },

  lifetimes: {
    attached() {
      // 组件初始化时获取当前页面信息
      const pages = getCurrentPages()
      if (pages.length > 0) {
        const currentPage = pages[pages.length - 1]
        this.updateNavBar(currentPage.route)
      }
    }
  },

  methods: {
    updateNavBar(pagePath) {
      if (!pagePath) return

      // 根据页面路径计算导航栏配置
      const pageConfig = this.getPageConfig(pagePath)

      this.setData({
        navTitle: pageConfig.title,
        showBackButton: pageConfig.showBackButton,
        showMenuButton: pageConfig.showMenuButton,
        showSearchIcon: pageConfig.showSearchIcon,
        showClearHistoryButton: pageConfig.showClearHistoryButton
      })
    },

    getPageConfig(pagePath) {
      const pages = getCurrentPages()
      const isSecondaryPage = pages.length > 1

      // 页面配置映射
      const pageConfigs = {
        'pages/index/index': {
          title: '校园新闻网',
          showBackButton: false,
          showMenuButton: true,
          showSearchIcon: false,
          showClearHistoryButton: false
        },
        'pages/search/search': {
          title: '搜索',
          showBackButton: false,
          showMenuButton: false,
          showSearchIcon: false,
          showClearHistoryButton: false
        },
        'pages/user/user': {
          title: '校园事务',
          showBackButton: false,
          showMenuButton: false,
          showSearchIcon: false,
          showClearHistoryButton: false
        },
        'pages/news/list': {
          title: '新闻列表',
          showBackButton: true,
          showMenuButton: false,
          showSearchIcon: false,
          showClearHistoryButton: false
        },
        'pages/news/detail': {
          title: '新闻详情',
          showBackButton: true,
          showMenuButton: false,
          showSearchIcon: false,
          showClearHistoryButton: false
        },
        'pages/login/login': {
          title: '登录',
          showBackButton: true,
          showMenuButton: false,
          showSearchIcon: false,
          showClearHistoryButton: false
        },
        'pages/favorites/favorites': {
          title: '我的收藏',
          showBackButton: true,
          showMenuButton: false,
          showSearchIcon: false,
          showClearHistoryButton: false
        },
        'pages/history/history': {
          title: '浏览历史',
          showBackButton: true,
          showMenuButton: false,
          showSearchIcon: false,
          showClearHistoryButton: this.checkHistoryNotEmpty()
        },
        'pages/education/education': {
          title: '教务中心',
          showBackButton: true,
          showMenuButton: false,
          showSearchIcon: false,
          showClearHistoryButton: false
        }
      }

      const config = pageConfigs[pagePath] || {
        title: '校园新闻网',
        showBackButton: isSecondaryPage,
        showMenuButton: !isSecondaryPage,
        showSearchIcon: false,
        showClearHistoryButton: false
      }

      return config
    },

    checkHistoryNotEmpty() {
      const app = getApp()
      return app.globalData.history.length > 0
    },

    handleLeftClick() {
      if (this.data.showBackButton) {
        // 返回上一页
        const pages = getCurrentPages()
        if (pages.length > 1) {
          wx.navigateBack()
        } else {
          // 如果没有上一页，跳转到首页
          wx.reLaunch({
            url: '/pages/index/index'
          })
        }
      }
    },

    handleRightClick() {
      if (this.data.showMenuButton) {
        // 跳转到菜单页面
        wx.navigateTo({
          url: '/pages/user/menu'
        })
      }
    },

    handleSearchClick() {
      wx.navigateTo({
        url: '/pages/search/search'
      })
    },

    handleClearHistory() {
      const that = this
      wx.showModal({
        title: '确认清空',
        content: '确定要清空所有浏览历史吗？此操作不可恢复。',
        success(res) {
          if (res.confirm) {
            const app = getApp()
            app.clearHistory()

            // 更新按钮显示状态
            that.setData({
              showClearHistoryButton: false
            })

            wx.showToast({
              title: '已清空所有历史记录',
              icon: 'success'
            })

            // 通知页面刷新
            that.triggerEvent('historyCleared')
          }
        }
      })
    }
  }
})
